<?php

namespace App\Http\Controllers\Admin\Stock;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\UserRepositoryEloquent;
use App\User;
use App\Models\Recharge;
use Breadcrumbs, Toastr,Config,Validator,DB;
use Illuminate\Http\Request;
use App\Models\StockActivity;

class StockActivityController extends BaseController{

    public function __construct(UserRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    public function index(Request $request){
        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','type'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }
        //$activities = [];
        $activities = StockActivity::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->orderBy('id','desc')->paginate(20);
        return view('admin.stock.activities',compact('activities','data'));
    }

 
    public function details(Request $request,$id){

        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','type'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }

        $activity=StockActivity::find_activity($id);

        $orders = $activity->orders()->where('status','=',1)->orderBy('id','desc')->get();
        $people=$orders->count();
        $total_price= $orders->sum('pay_money');

        $activity->people=$people;
        $activity->total_price=$total_price;
        // $codes = $orders->pluck('stock_code');
        // $codes =  $codes->unique();
        // $codes = implode(',',$codes->all());
        // $res = get_detail_data($codes);
        // $datas = collect($res);
        // $datas = $datas->keyBy('ticker');
        // foreach($orders as $k=>$order){
        //      $data = null;
        //     if(isset($datas[$order->stock_code])){
        //         $data = $datas[$order->stock_code];
        //     }
        //     if(!$data){
        //         continue;
        //     }
        //     $open_price = $data->openPrice;
        //     $order->open_price = $open_price;
        //     $order->lastPrice = $data->lastPrice;
           
        //     $yeild = sprintf("%.4f",(($data->lastPrice-$data->openPrice)/$data->lastPrice)*100);
        //     $order->yeild = $yeild;
        //     $orders[$k] = $order;
        // }
        $orders = $orders->sortByDesc("yield")->values();
       
        return view('admin.stock.details',compact('activity','orders','data'));
    }

}